package com.sqx.modules.errand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.dao.UserDao;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.cityInfo.entity.CityInfo;
import com.sqx.modules.cityInfo.service.CityInfoService;
import com.sqx.modules.creditRecord.entity.CreditRecord;
import com.sqx.modules.errand.dao.AdminDao;
import com.sqx.modules.errand.dao.ComplaintDao;
import com.sqx.modules.errand.dao.IllegalTypeDao;
import com.sqx.modules.errand.dao.IndentDao;
import com.sqx.modules.errand.entity.Indent;
import com.sqx.modules.errand.entity.MoneyDetails;
import com.sqx.modules.errand.entity.SysComplaint;
import com.sqx.modules.errand.entity.IllegalType;
import com.sqx.modules.errand.service.ComplaintService;
import com.sqx.modules.sys.entity.SysUserEntity;
import com.sqx.modules.sys.entity.SysUserRoleEntity;
import com.sqx.modules.sys.service.SysUserRoleService;
import com.sqx.modules.sys.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

@Service
public class ComplaintServiceImpl extends ServiceImpl<ComplaintDao, SysComplaint> implements ComplaintService {

    @Autowired
    private IllegalTypeDao illegalTypeDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private IndentDao indentDao;
    @Autowired
    private UserMoneyDetailsService userMoneyDetailsService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private SysUserService sysUserService;

    @Override
    public Result findAllComplaint(Long userId, Integer illegalId, Integer complaintState, Integer page, Integer limit) {
        Page<SysComplaint> pages = new Page<>(page, limit);
        PageUtils pageUtils = new PageUtils(baseMapper.findAppComplaint(pages, userId, illegalId, complaintState));
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result insertComplaint(SysComplaint sysComplaint) {
        //根据投诉类型查扣款金额
        Integer illegalId = sysComplaint.getIllegalId();
        IllegalType illegalType = illegalTypeDao.selectById(illegalId);
        BigDecimal forfeitMoney = illegalType.getForfeitMoney();
        //添加投诉记录
        Indent indent = indentDao.selectIndentByIndentNumber(sysComplaint.getIndentNumber());
        sysComplaint.setDeductMoney(forfeitMoney);
        sysComplaint.setCityId(indent.getCityId());
        sysComplaint.setComplaintTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        sysComplaint.setComplaintState(5);
        sysComplaint.setIndentType(indent.getIndentType());
        sysComplaint.setRiderUserId(indent.getRiderUserId());
        sysComplaint.setCityId(indent.getCityId());
        baseMapper.insert(sysComplaint);
        return Result.success();
    }

    @Override
    public Result findComplaint(Long userId, Long complaintId) {
        SysComplaint sysComplaint = baseMapper.findComplaint(complaintId);
        return Result.success().put("data", sysComplaint);
    }

    @Override
    public Result appealIndent(Long userId, SysComplaint sysComplaint) {
        sysComplaint.setComplaintState(2);
        sysComplaint.setAppealTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        baseMapper.updateById(sysComplaint);
        return Result.success();
    }

    @Override
    public IPage<SysComplaint> findAllAppeal(Integer page, Integer limit, SysComplaint complaint) {
        Page<SysComplaint> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return baseMapper.findAllComplaint(pages, complaint);

    }

    @Override
    public IPage<SysComplaint> getUserComplaint(Long userId, Integer page, Integer limit) {
        Page<SysComplaint> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }

        return baseMapper.getUserComplaint(pages, userId);

    }

    @Override
    public Result updateCustomerComplaint(Long complaintId, Integer complaintState) {
        if (complaintState == 1) {
            //投诉通过，状态改为1
            SysComplaint sysComplaint = new SysComplaint();
            sysComplaint.setComplaintId(complaintId);
            sysComplaint.setComplaintState(1);
            baseMapper.updateById(sysComplaint);
            //扣保证金
            SysComplaint sysComplaint1 = baseMapper.selectById(complaintId);
            UserEntity userEntity1 = userDao.selectById(sysComplaint1.getRiderUserId());
            BigDecimal subtract;

            if (sysComplaint1.getDeductMoney().doubleValue() > userEntity1.getCashDeposit().doubleValue()) {
                //如果保证金不够，则保证金改为0
                subtract = BigDecimal.valueOf(0);
            } else {
                //如果保证金足够，则扣除保证金
                subtract = userEntity1.getCashDeposit().subtract(sysComplaint1.getDeductMoney());
            }
            UserEntity userEntity = new UserEntity();
            userEntity.setUserId(sysComplaint1.getRiderUserId());
            userEntity.setCashDeposit(subtract);
            userDao.updateById(userEntity);
            //退保证金的记录
            UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
            userMoneyDetails.setUserType(2);
            userMoneyDetails.setUserId(sysComplaint1.getRiderUserId());
            userMoneyDetails.setTitle("扣除保证金");
            userMoneyDetails.setContent("被投诉订单，扣除保证金：" + sysComplaint1.getDeductMoney() + "元！");
            userMoneyDetails.setType(2);
            userMoneyDetails.setClassify(1);
            userMoneyDetails.setMoney(sysComplaint1.getDeductMoney());
            userMoneyDetails.setCreateTime(new SimpleDateFormat("yyyy-MM-dd :HH:mm:ss").format(new Date()));
            userMoneyDetailsService.save(userMoneyDetails);

        } else {
            //投诉未通过，修改投诉状态为6
            SysComplaint sysComplaint = new SysComplaint();
            sysComplaint.setComplaintId(complaintId);
            sysComplaint.setComplaintState(6);
            baseMapper.updateById(sysComplaint);
        }
        return Result.success();
    }

    @Override
    public Result updateComplaintState(SysComplaint sysComplaint) {
        baseMapper.updateById(sysComplaint);
        //如果申诉通过，则将罚金退回骑手余额
        if (sysComplaint.getComplaintState() == 4) {
            Long complaintId = sysComplaint.getComplaintId();
            SysComplaint sysComplaint1 = baseMapper.selectById(complaintId);
            UserEntity userEntity = userDao.selectById(sysComplaint1.getRiderUserId());
            BigDecimal subtract = userEntity.getCashDeposit().add(sysComplaint1.getDeductMoney());
            userEntity.setCashDeposit(subtract);
            userDao.updateById(userEntity);
            //添加一条退款记录到钱包表
            UserMoneyDetails moneyDetails = new UserMoneyDetails();
            moneyDetails.setUserId(sysComplaint1.getRiderUserId());
            moneyDetails.setTitle("申诉成功");
            moneyDetails.setContent("退还保证金：" + sysComplaint1.getDeductMoney());
            moneyDetails.setType(1);
            moneyDetails.setClassify(1);
            moneyDetails.setMoney(sysComplaint1.getDeductMoney());
            moneyDetails.setCreateTime(DateUtils.format(new Date()));
            userMoneyDetailsService.save(moneyDetails);
        }
        return Result.success();
    }

}
